package util;

import com.nanosl.lib.db.Manager;
import com.nanosl.lib.ui.NDialog;
import com.nanosl.lib.ui.NFrame;
import ent.Operator;
import ent.ViewPanel;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import main.MainView;
import ui.operator.PermissionView;

/**
 *
 * @author t
 */
public class SuperView extends NDialog {

    protected static MainView mainView = MainView.getInstance();
    protected Manager m = Manager.getInstance();

    public SuperView() {
        super(mainView, false);
        registerView();
    }

    private void registerView() {
        String className = getClass().getSimpleName();
        ViewPanel viewPanel = m.find(ViewPanel.class, className);
        if (viewPanel == null) {
            viewPanel = new ViewPanel(className);
            try {
                m.update(viewPanel);
            } catch (Exception ex) {
                Logger.getLogger(SuperView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void setVisible(boolean b) {
        m.clearCache();
        String className = getClass().getSimpleName();
        Operator operator = util.Data.getOperator();
        if (m.find(Operator.class).size() == 1 && operator.getViewPanelCollection().isEmpty()) {
            if (className.equals("PermissionView")) {
                super.setVisible(true);
                return;
            } else {
                PermissionView.display();
                return;
            }
        }
        if (isVisible()) {
            return;
        }
        if (b) {
            b = false;
            ViewPanel viewPanel = m.find(ViewPanel.class, className);
            Collection<ViewPanel> viewPanels = operator.getViewPanelCollection();
            b = viewPanels.contains(viewPanel);
            if (!b) {
                b = authView();
            }
            if (!b) {
                showError("You Do Not Have Authentication!");
            }
        }
        super.setVisible(b);
    }

    @Override
    protected NFrame getMainView() {
        return mainView;
    }
}
